using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class PraiseRecordDAL
    {
        public static int Save(PraiseRecordInfo info)
        {
            string sql = string.Format("insert into [praise_record]([SID],[ShareUserID],[PraiseUserID],[Type],[CreateDate]) values(@SID,@ShareUserID,@PraiseUserID,@Type,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@SID", info.SID),
                new SqlParameter("@ShareUserID", info.ShareUserID),
                new SqlParameter("@PraiseUserID", info.PraiseUserID),
                new SqlParameter("@Type", info.Type),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static bool IsPraise(int sid, int shareuserid, int praiseuserid, int type)
        {
            int ID = DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select ID from [praise_record] where SID={0} and ShareUserID={1} and PraiseUserID={2} and Type={3}", sid, shareuserid, praiseuserid, type)));
            return ID > 0 ? true : false;
        }
        public static List<dynamic> PraiseUser(int sid,int shareuserid, int type)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select [user].HeadImg,[user].NickName,[praise_record].CreateDate from [praise_record] left join [user] on [praise_record].PraiseUserID=[user].ID where SID={0} and ShareUserID={1} and Type={2}", sid, shareuserid, type));

            List<dynamic> list = new List<dynamic>();
            foreach (DataRow row in dt.Rows)
            {
                dynamic obj = new ExpandoObject();
                obj.HeadImg = DataConvert.ToString(row["HeadImg"]);
                obj.NickName = DataConvert.ToString(row["NickName"]);
                obj.CreateDate = DataConvert.ToDateTime(row["CreateDate"]).ToString("yyyy-MM-dd HH:mm");
                list.Add(obj);
            }
            return list;
        }
        public static PraiseRecordInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[praise_record]");
            return SimpleData(dr);
        }
        public static List<PraiseRecordInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[praise_record]");
            return ListSimpleData(dt);
        }

        public static List<PraiseRecordInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [praise_record]"));
            return ListSimpleData(dt);
        }
        public static int FindCountBySID(int sid, int shareuserid)
        {
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [praise_record] where SID={0} and ShareUserID={1}",sid,shareuserid)));
        }
        public static List<PraiseRecordInfo> FindListBySIDs(string sids)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(sids))
            {
                dt=CommonDAL.GetDataTable(string.Format("select * from [praise_record] where SID in ({0})", sids));
            }
            return ListSimpleData(dt);
        }
        private static PraiseRecordInfo SimpleData(DataRow dr)
        {
            PraiseRecordInfo info = new PraiseRecordInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.ShareUserID = DataConvert.ToInt(dr["ShareUserID"]);
                info.PraiseUserID = DataConvert.ToInt(dr["PraiseUserID"]);
                info.Type = DataConvert.ToInt(dr["Type"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static PraiseRecordInfo AllData(DataRow dr)
        {
            PraiseRecordInfo info = new PraiseRecordInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.ShareUserID = DataConvert.ToInt(dr["ShareUserID"]);
                info.PraiseUserID = DataConvert.ToInt(dr["PraiseUserID"]);
                info.Type = DataConvert.ToInt(dr["Type"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<PraiseRecordInfo> ListSimpleData(DataTable dt)
        {
            List<PraiseRecordInfo> list = new List<PraiseRecordInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<PraiseRecordInfo> ListAllData(DataTable dt)
        {
            List<PraiseRecordInfo> list = new List<PraiseRecordInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}